package com.acxy.pay.alipay;

import com.acxy.ants.common.kit.StrKit;
import com.acxy.ants.core.annotation.Value;
import com.acxy.ants.core.kit.LogKit;
import com.acxy.ants.resful.annotation.API;
import com.acxy.ants.resful.annotation.Controller;
import com.acxy.ants.resful.annotation.GET;
import com.acxy.ants.resful.annotation.POST;
import com.alipay.api.*;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-11-06
 */
@Controller("alipay")
public class AliPayController {

    // 商户appid
    @Value("ali.appId")
    private String appId;
    // 私钥 pkcs8格式的
    @Value("ali.privateKey")
    private String privateKey;
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    @Value("ali.notifyurl")
    private String notifyUrl;
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    @Value("ali.returnurl")
    private String returnUrl;
    // 支付宝公钥
    @Value("ali.publicKey")
    private String publicKey;

    // 请求网关地址
    private String url = "https://openapi.alipay.com/gateway.do";
    // 编码
    private String charset = "UTF-8";
    // 返回格式
    private String format = "json";
    // 日志记录目录
    private String logPath = "/log";
    // RSA2
    private String signType = "RSA2";


    @GET("/pay")
    public void mobilePay(int type, String t, HttpServletResponse response) throws IOException {
        AlipayClient client = new DefaultAlipayClient(url, appId, privateKey, format, charset, publicKey, signType);

        AlipayRequest aliPayRequest = null;
        //H5手机支付
        if (type == 1) {
            aliPayRequest = new AlipayTradeWapPayRequest();
            // 封装请求支付信息
            AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
            model.setOutTradeNo("wx" + StrKit.randomUUID());
            model.setSubject("支付宝PC手机支付测试"); //订单名称
            model.setTotalAmount("0.01");
            model.setBody("支付描述啊啊啊");
            model.setTimeoutExpress("2m");
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            aliPayRequest.setBizModel(model);
        } else {
            aliPayRequest = new AlipayTradePagePayRequest();
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.setOutTradeNo("wx" + StrKit.randomUUID());
            model.setSubject("支付宝PC手机支付测试"); //订单名称
            model.setTotalAmount("0.01");
            model.setBody("支付描述啊啊啊");
            model.setTimeoutExpress("2m");
            model.setEnablePayChannels("balance,moneyFund,pcredit,bankPay");
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            boolean blank = StrKit.isBlank(t);
            model.setQrPayMode(blank ? "2" : t);
            aliPayRequest.setBizModel(model);
        }


        // 设置异步通知地址
        aliPayRequest.setNotifyUrl(notifyUrl);
        // 设置同步地址
        aliPayRequest.setReturnUrl(returnUrl);

        // form表单生产
        // 调用SDK生成表单
        String form = null;
        try {
            form = client.pageExecute(aliPayRequest).getBody();
            response.setContentType("text/html;charset=" + charset);
            response.getWriter().write(form);//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (AlipayApiException e) {
            e.printStackTrace();
            System.out.println("###:" + form);
        }
    }

    @POST("/notify_url")
    public void notifyUrl() {
        System.out.println("授权回调地址来了!");
    }

    @GET("/return_url")
    public Object returnUrl() {
        System.out.println("支付宝页面跳转同步通知页面!");
        return "ok";
    }

    @GET("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userAgent = request.getHeader("User-Agent");
        System.out.println("userAgent:" + userAgent);
        if (userAgent.indexOf("MicroMessenger") != -1) { // 来源微信扫描
            response.sendRedirect("https://payapp.weixin.qq.com/qrpay/order/home2?key=idc_CHNDVI_i4DEazNZIWOr28oD3TflHw--");
        } else if (userAgent.indexOf("AlipayClient") != -1) { // 来源支付宝扫描
            response.sendRedirect("https://qr.alipay.com/stx05389qrzcxwdn9iajhdc");
        }
    }
}
